package zhuyun.code

import java.awt.Color
import java.awt.Graphics
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.ButtonGroup
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JRadioButton

fun main(args:Array<String>){
    ChessBoard()
}
data class Point(val x:Int,val y:Int)
interface ChessPieces{
    fun downPieces(g:Graphics,pt: Point)
}
class WhitePieces: ChessPieces {
    override fun downPieces(g: Graphics, pt: Point) {
        g.color = Color.WHITE
        g.fillOval(pt.x,pt.y,30,30)
    }

}
class BlackPieces: ChessPieces {
    override fun downPieces(g: Graphics, pt: Point) {
        g.color = Color.BLACK
        g.fillOval(pt.x,pt.y,30,30)
    }

}
class WeiqiFactory{
    private val qz = mutableListOf<ChessPieces>()
    init {
        val whitePieces = WhitePieces()
        val blackPieces = BlackPieces()
        qz.add(whitePieces)
        qz.add(blackPieces)
    }
    fun getChessPieces(type:String): ChessPieces?{
        return when(type){
            "w"->{
                qz[0]
            }
            "b"->{
                qz[1]
            }
            else->{
                null
            }

        }
    }
}
class ChessBoard:MouseAdapter(){
    private var wf: WeiqiFactory = WeiqiFactory()
    private var f:JFrame = JFrame("享元模式在五子棋游戏中的应用")
    private var wz = JRadioButton("白子")
    private var bz = JRadioButton("黑子",true)
    private lateinit var g:Graphics
    private val x = 50
    private val y = 50
    private val w = 40
    private val rw = 400
    init {
        f.setBounds(100,100,500,550)
        f.isVisible = true
        f.isResizable = false
        f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        val southJP = JPanel()
        f.add("South",southJP)
        val group = ButtonGroup()
        group.add(wz)
        group.add(bz)
        southJP.add(wz)
        southJP.add(bz)
        val centerJP = JPanel()
        centerJP.layout = null
        centerJP.setSize(500,500)
        centerJP.addMouseListener(this)
        f.add("Center", centerJP)
        Thread.sleep(500)
        g = centerJP.graphics
        g.color = Color.BLUE
        g.drawRect(x,y,rw,rw)
        for(i in 1 until 10){
            g.drawLine(x+i*w,y,x+i*w,y+rw)
            g.drawLine(x,y+i*w,x+rw,y+i*w)
        }

    }
    override fun mouseClicked(e: MouseEvent?) {
        val ex = e!!.x
        val ey = e!!.y
        var vl = 50
        var hl:Int
        while(vl<=450&&Math.abs(ex-vl)>20){
            vl+=40
        }
        if(vl>450){
            return
        } else {
            if(Math.abs(ex-vl)>15){
                return
            } else {
                hl = 50
                while(hl<=450&&Math.abs(ey-hl)>20){
                    hl+=40
                }
                if(hl>450){
                    return
                } else {
                    if(Math.abs(ey-hl)>15){
                        return
                    }
                    else {
                        if((ex-vl)*(ex-vl)+(ey-hl)*(ey-hl)<=225){

                        } else {
                            return
                        }
                    }
                }
            }
        }
        val pieceXPos = vl - 15
        val pieceYPos = hl - 15
        val pt = Point(pieceXPos, pieceYPos)
        if(wz.isSelected){
            val chessPieces = wf.getChessPieces("w")
            chessPieces!!.downPieces(g,pt)
        } else {
            if(bz.isSelected){
                val chessPieces = wf.getChessPieces("b")
                chessPieces!!.downPieces(g,pt)
            }
        }


    }
}